/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.device.server.processor;

import java.util.Calendar;
import java.util.SimpleTimeZone;

import com.bugull.farm.core.cache.PropertiesCache;
import com.bugull.farm.core.frame.structure.Frame;
import com.bugull.farm.core.handler.Processor;
import com.bugull.farm.core.utils.ByteUtil;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

import org.apache.logging.log4j.*;

/**
 * For command: CMD_DEV_HEARTBEAT
 * 
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class DeviceHeartbeatProcessor implements Processor {

    private static Logger logger = LogManager.getLogger(DeviceHeartbeatProcessor.class);
    @Override
    public void process(ChannelHandlerContext ctx, Frame frame) throws Exception {
    	//心跳回复间隔
        byte[] intervalBytes = ByteUtil.fromShort((short)PropertiesCache.getInstance().getInt("dev.heartbeat.interval").intValue());

        int time = (int)(System.currentTimeMillis()/1000);

        //UTC毫秒数，从1970年1月1日至今的毫秒数
        byte[] current_timeBytes = ByteUtil.fromInt(time);
        //回复消息
        frame.setBytes("interval", intervalBytes);
        frame.setBytes("time", current_timeBytes);
        ctx.writeAndFlush(frame.getFastResponse());
    }

}
